package com.thinksns.sociax.thinksnsbase.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * Created by hedong on 16/2/19.
 */
public class BaseFragment extends Fragment implements View.OnClickListener {
    public static final int STATE_NONE = 0;
    public static final int STATE_REFRESH = 1;
    public static final int STATE_LOADMORE = 2;
    public static final int STATE_NOMORE = 3;
    public static final int STATE_PRESSNONE = 4;// 正在下拉但还没有到刷新的状态
    public int mState = STATE_NONE;

    protected LayoutInflater mInflater;
    protected ViewGroup rootView;
    protected Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mInflater = inflater;
        if (rootView != null) {
            if (rootView.getParent() != null)
                ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
        rootView = (ViewGroup) inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, rootView);
        context = getActivity();
        if (needEventBus()) {
            EventBus.getDefault().register(this);
        }
        initReceiver();
        initIntentData();
        initPresenter();
        initView(rootView);
        initListener();
        initData();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        destroyReceiver();
        if (needEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }


    protected int getLayoutId() {
        return 0;
    }

    protected View inflateView(int resId) {
        return this.mInflater.inflate(resId, null);
    }


    @Override
    public void onClick(View v) {

    }

    /**
     * 7.21.2017 兼容4.5以前版本
     *
     * @return
     */
    protected void initIntentData() {

    }

    protected void initView(View view) {

    }

    protected void initPresenter() {
    }

    protected void initListener() {
    }

    protected void initReceiver() {
    }

    protected void destroyReceiver() {
    }

    protected void initData() {
    }

    protected View findViewById(int id) {
        return rootView.findViewById(id);
    }

    /**
     * 增加方法是否用eventBus，为兼 4.5前，默认返回false
     *
     * @return
     */
    protected boolean needEventBus() {
        return false;
    }
}
